package com.wswy.wzcx.model;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable.Creator;
import android.text.TextUtils;
import com.google.gson.annotations.SerializedName;
import com.wswy.wzcx.R;
import com.wswy.wzcx.utils.StringUtils;

import java.util.Date;

public class TrafficViolationInfo implements android.os.Parcelable {

  @SerializedName("id")
  public long id;

  @SerializedName("carNo")
  public String carNo;

  @SerializedName("fineDeductPoints")
  public int fineDeductPoints;

  @SerializedName("fineDetail")
  public String fineDetail;

  @SerializedName("fineFee")
  public String fineFee;

  @SerializedName("fineLocation")
  public String fineLocation;

  @SerializedName("fineTime")
  public String fineTime;

  @SerializedName("fineCity")
  public String fineCity;

  @SerializedName("fineProvince")
  public String province;

    //since v3.1.0
    @SerializedName("status")
    public String statusName;

    @SerializedName("lan")
    public double lon;
    @SerializedName("lat")
    public double lat;

    @SerializedName("createTime")
    public long createTime;
    @SerializedName("uniqueMd5Id")
    public String uniqueMd5Id;

    private transient String statusNameDesc=null;
    private transient int fineOverdueDays = -1; //违章天数

    public int getFeeValue(){
      try{
        return Integer.parseInt(fineFee);
      }catch (Throwable e){
        e.printStackTrace();
      }
      return 0;
    }

    public String getStatusNameDesc(Context context){
      if (statusNameDesc != null){
        return statusNameDesc;
      }
      int strId = R.string.result_status_unsupported;
      if (!TextUtils.isEmpty(statusName)){
        switch (statusName){
          case "10":
            strId = R.string.result_status_can;
            break;
          case "20":
            strId = R.string.result_status_unsupported;
            break;
          case "30":
            strId = R.string.result_status_processing;
            break;
          case "40":
            strId = R.string.result_status_complete;
            break;
            default:
              strId = R.string.result_status_unsupported;
              break;
        }
      }
      statusNameDesc = context.getString(strId);

      return statusNameDesc;
    }

    public int getFineOverdueDays(){

      if (fineOverdueDays == -1){
        fineOverdueDays = 0;
        Date date = StringUtils.parseTime(fineTime);
        if (date != null){
          //
          long diff = new Date().getTime() - date.getTime();
          fineOverdueDays=  (int) (diff / (1000 * 24 * 60 * 60));
        }
      }
      return fineOverdueDays;
    }


  public TrafficViolationInfo() {
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
      dest.writeLong(this.id);
    dest.writeString(this.carNo);
    dest.writeInt(this.fineDeductPoints);
    dest.writeString(this.fineDetail);
    dest.writeString(this.fineFee);
    dest.writeString(this.fineLocation);
    dest.writeString(this.fineTime);
    dest.writeString(this.fineCity);
    dest.writeString(this.province);
    dest.writeString(this.statusName);
    dest.writeDouble(this.lon);
    dest.writeDouble(this.lat);
    dest.writeLong(this.createTime);
    dest.writeString(this.uniqueMd5Id);
  }

  protected TrafficViolationInfo(Parcel in) {
      this.id = in.readLong();
    this.carNo = in.readString();
    this.fineDeductPoints = in.readInt();
    this.fineDetail = in.readString();
    this.fineFee = in.readString();
    this.fineLocation = in.readString();
    this.fineTime = in.readString();
    this.fineCity = in.readString();
    this.province = in.readString();
    this.statusName = in.readString();
    this.lon = in.readDouble();
    this.lat = in.readDouble();
    this.createTime = in.readLong();
    this.uniqueMd5Id = in.readString();
  }

  public static final Creator<TrafficViolationInfo> CREATOR = new Creator<TrafficViolationInfo>() {
    @Override
    public TrafficViolationInfo createFromParcel(Parcel source) {
      return new TrafficViolationInfo(source);
    }

    @Override
    public TrafficViolationInfo[] newArray(int size) {
      return new TrafficViolationInfo[size];
    }
  };
}
